package com.da.lon.wang.xlg.adapter;

import android.content.Context;
import android.graphics.Paint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.da.lon.wang.xlg.MyApplication;
import com.da.lon.wang.xlg.R;
import com.da.lon.wang.xlg.bean.PartyInfoResultBean.PartyProduct;

import java.util.ArrayList;


public class PartyInfoAdapter extends BaseAdapter {
    private Context context;
    private LayoutInflater inflater;
    private ArrayList<PartyProduct> list;

    public PartyInfoAdapter(Context context, ArrayList<PartyProduct> list) {
        this.context = context;
        inflater = LayoutInflater.from(context);
        this.list = list;
    }

    @Override
    public int getCount() {
        return list != null ? list.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        try {
            if (convertView == null) {
                convertView = inflater.inflate(R.layout.party_info_item, null);
            }
            Holder holder = new Holder();
            holder.search_grid_img = (ImageView) convertView
                    .findViewById(R.id.search_grid_img);
            holder.search_grid_name = (TextView) convertView
                    .findViewById(R.id.search_grid_name);
            holder.search_grid_pricespe = (TextView) convertView
                    .findViewById(R.id.search_grid_pricespe);
            holder.xiadan_ll = (LinearLayout) convertView
                    .findViewById(R.id.xiadan_ll);
            holder.search_grid_price = (TextView) convertView.findViewById(R.id.search_grid_price);

            holder.look_tv = (TextView) convertView.findViewById(R.id.look_tv);

            if (position < list.size() && list.get(position) != null) {
                MyApplication.loadImageByPicasso(context, list.get(position)
                        .getBigimage(), null, holder.search_grid_img, 200, 200, 0);

                if (list.get(position).getZkl() > 0) {
                    holder.look_tv.setText(list.get(position).getZkl() + "折");
                } else {
                    holder.xiadan_ll.setVisibility(View.GONE);
                }

                holder.search_grid_name.setText(list.get(position).getName());
                holder.search_grid_pricespe.setText("￥" + list.get(position).getSeal_price());
                holder.search_grid_price.setText("￥" + list.get(position).getPrice());
                holder.search_grid_price.getPaint().setFlags(
                        Paint.STRIKE_THRU_TEXT_FLAG);
            }
            if (convertView.getTag() == null) {
                convertView.setTag(holder);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return convertView;
    }

    class Holder {
        ImageView search_grid_img;
        TextView look_tv;
        LinearLayout xiadan_ll;
        TextView search_grid_name, search_grid_pricespe, search_grid_price;
    }
}
